﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 02/01/2009
// Time: 17:10
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using csharp.common;

namespace csharp.rad.automaPlus
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : KryptonForm
	{
		csharp.rad.security.WinApp MyApp = new csharp.rad.security.WinApp();
		private Configuration config = new Configuration();
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		private void MainFormLoad(object sender, EventArgs e)
		{
			//Valida sistema
			general.Company company = new general.Company();
			
			if (!MyApp.Initialize(System.Reflection.Assembly.GetExecutingAssembly()))
			{
				KryptonMessageBox.Show("Banco de dados não conectado!" + csharp.common.Database.result.ErrorMessage);
				this.Close();
				return;
			}
			
			if (!System.IO.File.Exists(config.path))
				config.doSerialization();
			
			config.deserialize();
			
			if (config.showFormChooseCompany)
			{
				frmChooseCompany form = new frmChooseCompany();
				
				showForm(form, true);
			}
			
			company.id = config.defaultCompanyId;
			
			if (!company.get())
			{
				KryptonMessageBox.Show("Empresa padrão não cadastrada!\nContacte o suporte!");
				System.Environment.Exit(System.Environment.ExitCode);
			}
			csharp.Information.companyId = company.id;
			csharp.Information.companyName = company.person.name;
			
			csharp.Information.userId = MyApp.ApplicationUser.Id;
			csharp.Information.userName = MyApp.ApplicationUser.Name;
			
			lblUserName.Text = MyApp.ApplicationUser.Name;
			lblCompanyName.Text = csharp.Information.companyName;
			//kryptonButton1.Visible = csharp.Information.userName.ToUpper().Equals("DEVELOPER");	
		}
		
		private void showForm(KryptonForm form, bool modal)
		{
			try
			{
				form.ShowDialog();
			}
			finally
			{
				form.Dispose();
			}			
		}		
		
		private void TsmiUtiGerenciarUsuariosClick(object sender, EventArgs e)
		{
			MyApp.SecurityManager();
		}
		
		private void TsmiFinanRegisterGroupClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterGroup form = new finan.entries.frmRegisterGroup();
			
			showForm(form, true);
		}
			
		private void TsmiFinanRegisterSubGroupClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterSubGroup form = new finan.entries.frmRegisterSubGroup();
			
			showForm(form, true);
		}
		
		private void TsmiFinanRegisterClassClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterClass form = new finan.entries.frmRegisterClass();
			
			showForm(form, true);
		}
		
		private void TsmiFinanRegisterSubClassClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterSubClass form = new finan.entries.frmRegisterSubClass();
			
			showForm(form, true);
		}
		
		private void TsmiFinanRegisterBillClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterBill form = new finan.entries.frmRegisterBill();
			
			showForm(form, true);
		}
		
		private void TsmiRegisterCustomerClick(object sender, EventArgs e)
		{
			general.frmRegisterCustomer form = new general.frmRegisterCustomer();
			
			showForm(form, true);
		}
		
		private void TsmiRegisterWarehouseClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterWarehouse form = new csharp.rad.general.entries.frmRegisterWarehouse();
			
			showForm(form, true);
		}
		
		private void TsmiRegisterEmployeClick(object sender, EventArgs e)
		{
			general.frmRegisterEmployee form = new csharp.rad.general.frmRegisterEmployee();
			
			showForm(form, true);
		}
		
		private void TsmiRegisterCompanyClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterCompany form = new csharp.rad.general.entries.frmRegisterCompany();
			
			showForm(form, true);
		}
		
		private void TsmiChooseCompanyClick(object sender, EventArgs e)
		{
			frmChooseCompany form = new frmChooseCompany();
			
			showForm(form, true);
			lblCompanyName.Text = csharp.Information.companyName;
		}
		
		private void TsmiMessageOfDayClick(object sender, EventArgs e)
		{
			KryptonMessageBox.Show(new common.MessagesFunny().getMessageOfDay());
		}
		
		private void TsmiProductGroupClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterProductGroup form = new csharp.rad.general.entries.frmRegisterProductGroup();
			
			showForm(form, true);
		}
		
		private void TsmiProductSubGroupClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterProductSubGroup form = new csharp.rad.general.entries.frmRegisterProductSubGroup();
			
			showForm(form, true);			
		}
		
		private void TsmiProductMarkClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterProductMark form = new csharp.rad.general.entries.frmRegisterProductMark();
			
			showForm(form, true);				
		}
		
		private void TsmiProductSectionClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterProductSection form = new csharp.rad.general.entries.frmRegisterProductSection();
			
			showForm(form, true);
		}
		
		private void TsmiProductTypeClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterProductType form = new csharp.rad.general.entries.frmRegisterProductType();
			
			showForm(form, true);			
		}
		
		private void TsmiCostCenterClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterCostCenter form = new csharp.rad.finan.entries.frmRegisterCostCenter();
			
			showForm(form, true);
		}
		
		private void TsmiBankClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterBank form = new csharp.rad.finan.entries.frmRegisterBank();
			
			showForm(form, true);
		}
		
		private void TsmiBillToReceiveClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterBpr form = new csharp.rad.finan.entries.frmRegisterBpr();
			
			form.bill_type = CONSTANTS.BILL_TYPE.TORECEYCE;
			
			showForm(form, true);
		}
		
		private void StatusToolStripMenuItem1Click(object sender, EventArgs e)
		{
			general.frmRegisterStatus form = new csharp.rad.general.frmRegisterStatus();
			
			showForm(form, true);
		}
		
		private void TsmiTypeClick(object sender, EventArgs e)
		{
			general.frmRegisterType form = new csharp.rad.general.frmRegisterType();
			
			showForm(form, true);
		}
		
		private void TsmiBillToPayClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterBpr form = new csharp.rad.finan.entries.frmRegisterBpr();
			
			form.bill_type = CONSTANTS.BILL_TYPE.TOPAY;
			
			showForm(form, true);			
		}
		
		private void KryptonButton1Click(object sender, EventArgs e)
		{
			finan.entries.frmViewBillPlan form = new csharp.rad.finan.entries.frmViewBillPlan();
			
			showForm(form, true);
		}
		
		private void TsmiUtiBackupClick(object sender, EventArgs e)
		{
			string folderBackup = Application.StartupPath+"\\backup";
			
			if (!System.IO.Directory.Exists(folderBackup))
			{
				try
				{
					System.IO.Directory.CreateDirectory(folderBackup);
				}
				catch
				{
					KryptonMessageBox.Show("Por favor crie o diretório para backup!\nDiretório:"+folderBackup);
					return;
				}
			}
			common.DatabaseBackup form = new common.DatabaseBackup(folderBackup);

			showForm(form, true);			
		}
		
		private void TsmiRegisterNeighborhoodClick(object sender, EventArgs e)
		{
			general.frmRegisterNeighborhood form = new csharp.rad.general.frmRegisterNeighborhood();
			
			showForm(form, true);
		}
		
		private void TsmiRegisterCityClick(object sender, EventArgs e)
		{
			general.frmRegisterCity form = new csharp.rad.general.frmRegisterCity();
			
			showForm(form, true);
		}
		
		private void TsmiRegisterStateClick(object sender, EventArgs e)
		{
			general.frmRegisterState form = new csharp.rad.general.frmRegisterState();
			
			showForm(form, true);
		}
		
		private void TsmiRegisterCountryClick(object sender, EventArgs e)
		{
			general.frmRegisterCountry form = new csharp.rad.general.frmRegisterCountry();
			
			showForm(form, true);
		}
		
		private void TsmiBillMvtClick(object sender, EventArgs e)
		{
			finan.entries.frmRegisterBillMvt form = new csharp.rad.finan.entries.frmRegisterBillMvt();
			
			showForm(form, true);			
		}
		
		private void TsmiResearchBillToReceiveClick(object sender, EventArgs e)
		{
			finan.researchs.frmResearchBpr form = new csharp.rad.finan.researchs.frmResearchBpr();
			
			form.bill_type = CONSTANTS.BILL_TYPE.TORECEYCE;
			showForm(form, true);
		}
		
		private void TsmiAgendaClick(object sender, EventArgs e)
		{
			general.views.frmagenda form = new csharp.rad.general.views.frmagenda();
			showForm(form, true);
		}
		
		private void TsmiSupplierClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterSupplier form = new csharp.rad.general.entries.frmRegisterSupplier();
			showForm(form, true);
		}
		
		private void ProdutosToolStripMenuItemClick(object sender, EventArgs e)
		{
			general.entries.frmRegisterProduct form = new csharp.rad.general.entries.frmRegisterProduct();
			showForm(form, true);
		}
		
		private void TsmbLogoutClick(object sender, EventArgs e)
		{
			DialogResult resp = KryptonMessageBox.Show("Deseja fazer logout?"
			                                    , "   Sair do sistema",
			                                    MessageBoxButtons.YesNo,
			                                    MessageBoxIcon.Question,
			                                    MessageBoxDefaultButton.Button2);

			if (resp == DialogResult.Yes)
			{
				//userValido= false;
				Application.Restart();
			}			
		}
		
		private void TsmiStockClick(object sender, EventArgs e)
		{
			general.views.frmStock form = new csharp.rad.general.views.frmStock();
			
			showForm(form, true);
		}
		
		void TsmiPurchaseClick(object sender, EventArgs e)
		{
			finan.entries.frmPurchases form = new csharp.rad.finan.entries.frmPurchases();
			showForm(form, true);
		}	
		
		void TsmiResearchBillToPayClick(object sender, EventArgs e)
		{
			finan.researchs.frmResearchBpr form = new csharp.rad.finan.researchs.frmResearchBpr();
			
			form.bill_type = CONSTANTS.BILL_TYPE.TOPAY;
			showForm(form, true);
		}
		
		private void TsmiResearchPurchaseClick(object sender, EventArgs e)
		{
			finan.researchs.frmResearchPurchase form = new csharp.rad.finan.researchs.frmResearchPurchase();
			
			showForm(form, true);
		}
	 }
}
